package com.example.forecastweather;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView cityText;
	private TextView condDescr;
	private TextView temp;
	private TextView pressLab, press, humLab, hum, windLab, windSpeed, windDeg;
	private ImageView condIcon;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String city = "London.UK";

		cityText = (TextView) findViewById(R.id.cityText);
		condDescr = (TextView) findViewById(R.id.condDescr);
		temp = (TextView) findViewById(R.id.temp);
		press = (TextView) findViewById(R.id.press);
		pressLab = (TextView) findViewById(R.id.pressLab);
		humLab = (TextView) findViewById(R.id.humLab);
		hum = (TextView) findViewById(R.id.hum);
		windLab = (TextView) findViewById(R.id.windLab);
		windSpeed = (TextView) findViewById(R.id.windSpeed);
		windDeg = (TextView) findViewById(R.id.windDeg);
		condIcon = (ImageView) findViewById(R.id.condIcon);

		JSONWeatherTask task = new JSONWeatherTask();
		task.execute(new String[] { city });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
		@Override
		protected Weather doInBackground(String... params) {
			// TODO Auto-generated method stub
			Weather weather = new Weather();
			String data = ((new WeatherHttpClient()).getDataWeather(params[0]));
			try {
				weather = JSONWeatherParse.getWeather(data);
				// khoi phuc icon
				weather.iconData = ((new WeatherHttpClient()
						.getImage(weather.current.getIcon())));
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return weather;
		}

		@Override
		protected void onPostExecute(Weather weather) {
			// TODO Auto-generated method stub
			super.onPostExecute(weather);

			if (weather.iconData != null && weather.iconData.length > 0) {
				Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0,
						weather.iconData.length);
				condIcon.setImageBitmap(img);
			}
			// ERROR

			cityText.setText(weather.location.getCity() + ","
					+ weather.location.getCountry());

			condDescr.setText(weather.current.getMain() + "("
					+ weather.current.getDescr() + ")");
			// Log.d("TEST", weather.current.getDescr());
			temp.setText(" " + Math.round(weather.temp.getTemp() - 273) + "oC");
			hum.setText(" " + weather.current.getHumidity() + "%");
			press.setText(" " + weather.current.getPressure() + "hPa");
			windSpeed.setText(" " + weather.wind.getSpeed() + "%");
			windDeg.setText(" " + weather.wind.getDeg() + "o");

		}

	}

}
